package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/8/1 9:31
 */
@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;


    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try {
            Long userId = request.getUserId();
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            Example example = new Example(Order.class);
            Example.Criteria criteria = example.createCriteria();
            if (userId != null) {
                criteria.andEqualTo("userId", userId);
            }
            List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();

            //查询当前用户所有订单
            List<Order> orders = orderMapper.selectByExample(example);
            for (Order order : orders) {
                String orderId = order.getOrderId();
                //根据每个订单的订单id,查询当前订单所有商品
                OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
                orderDetailInfo = orderConverter.order2detail(order);
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
                List<OrderItemDto> orderItemDtos = new ArrayList<>();
                for (OrderItem orderItem : orderItems) {
                    OrderItemDto orderItemDto = new OrderItemDto();
                    orderItemDto = orderConverter.item2dto(orderItem);
                    orderItemDtos.add(orderItemDto);
                }
                orderDetailInfo.setOrderItemDto(orderItemDtos);
                //根据每个订单的订单id,查询当前订单物流
                OrderShipping orderShipping = orderShippingMapper.selectByOrderId(orderId);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
                orderDetailInfos.add(orderDetailInfo);
            }
            //分页
            PageInfo<OrderDetailInfo> orderDetailInfoPageInfo = new PageInfo<>(orderDetailInfos);
            response.setData(orderDetailInfos);
            response.setTotal(orderDetailInfoPageInfo.getTotal());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("IProductDetailImpl.getAllProduct occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);

        }

        return response;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        return null;
    }

    @Override
    public OrderDetail orderInfo(OrderDetailRequest request) {
        OrderDetail orderDetail = new OrderDetail();

        String orderId = request.getOrderId();
        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);

        List<OrderItemDto> orderItemDtos = new ArrayList<>();
        Double orderTotal = 0.0;
        for (OrderItem orderItem : orderItems) {
            OrderItemDto orderItemDto = new OrderItemDto();
            orderItemDto = orderConverter.item2dto(orderItem);
            orderItemDtos.add(orderItemDto);
            orderTotal += (orderItem.getPrice() * orderItem.getNum());
        }
        //1
        orderDetail.setGoodsList(orderItemDtos);
        //2
        orderDetail.setOrderTotal(BigDecimal.valueOf(orderTotal));
        //3
        Order order = orderMapper.selectByOrderId(orderId);
        orderDetail.setOrderStatus(order.getStatus());
        //4
        OrderShipping orderShipping = orderShippingMapper.selectByOrderId(orderId);
        orderDetail.setStreetName(orderShipping.getReceiverAddress());
        orderDetail.setTel(orderShipping.getReceiverPhone());
        orderDetail.setUserId(order.getUserId());

        return orderDetail;

    }
}
